package off_imp

import (
	
	"bp/conf"
	"bp/db"
	"bp/internal/service/com"
	"bp/internal/service/entity"
	"bp/internal/util"
	offGrpc "bp/rpc/gen/backendapi/off"
	messageGrpc "bp/rpc/gen/shared/message"
	"context"
	"fmt"
	"github.com/golang/protobuf/ptypes/empty"
	"go.uber.org/zap"
	"google.golang.org/grpc"
)

const (
	EntityName            string = "优惠试算"
	offServiceName string = "bp.backendapi.OffOpService"
)

var (
	// allowPagerMethods              []string
	// allowBilinTokenMethods         []string
	allowAuthorizationTokenMethods []string
)

type OffBackendApiImp struct {
	dao       *db.PgDao
	slog      *zap.SugaredLogger
	vars      *conf.VarsSection
	entityHub *entity.EntityHub
	comHub    *com.ComHub
	// cacher *cache.Adapter
	offGrpc.OffOpServiceServer
}

// NewOffTrialMWebApi initialization
func New(
	slog *zap.SugaredLogger,
	pgdao *db.PgDao,
	vars *conf.VarsSection,
	s *grpc.Server,
	entityHub *entity.EntityHub,
	comHub *com.ComHub,
	// adp *cache.Adapter,
) (obs *OffBackendApiImp) {
	obs = &OffBackendApiImp{
		dao:       pgdao,
		slog:      slog.With("module", "OffBackendApi"),
		vars:      vars,
		entityHub: entityHub,
		comHub:    comHub,
		// cacher:           adp,
	}

	RegisterAllowAuthorizationToken()

	offGrpc.RegisterOffOpServiceServer(s, obs)
	slog.Info("couponGrpc register")
	
	return
}

// RegisterAllowAuthorizationToken 注册允许访问AuthorizationTokenMW的URL
func RegisterAllowAuthorizationToken() {
	allowAuthorizationTokenMethods = append(allowAuthorizationTokenMethods,
		fmt.Sprintf("/%s/TransactLock", offServiceName),
		fmt.Sprintf("/%s/GetOffTransactionInfo", offServiceName),
		fmt.Sprintf("/%s/GetOffApportionmentInfo", offServiceName),
	)
}

func (s *OffBackendApiImp) AllowAuthorizationTokenMW(method string) bool {
	return util.StringContains(allowAuthorizationTokenMethods, method)
}

func (s *OffBackendApiImp) PingOff(ctx context.Context, _ *empty.Empty) (res *messageGrpc.PingRes, err error) {
	res = &messageGrpc.PingRes{
		Msg: "off:ok",
	}
	return
}



func (s *OffBackendApiImp) GetOffTransactionInfo(ctx context.Context, req *offGrpc.GetOffTransactionInfoReq) (res *offGrpc.GetOffTransactionInfoRes, err error) {
	// token := s.getCtxToken(ctx)
	// s.slog.Infof("token=>%+v", token)
	// s.slog.Infof("req=>%+v", req)
	res = &offGrpc.GetOffTransactionInfoRes{}
	return
}

func (s *OffBackendApiImp) GetOffApportionmentInfo(ctx context.Context, req *offGrpc.GetOffApportionmentInfoReq) (res *offGrpc.GetOffApportionmentInfoRes, err error) {
	// token := s.getCtxToken(ctx)
	// s.slog.Infof("token=>%+v", token)
	// s.slog.Infof("req=>%+v", req)
	res = &offGrpc.GetOffApportionmentInfoRes{}
	return
}
